package com.apress.gerber.mytest;

import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.opengl.GLSurfaceView;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;

import com.apress.gerber.mytest.common.MediaPlayerWrapper;

import java.io.IOException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import tv.danmaku.ijk.media.player.IMediaPlayer;

public class NativeMediaActivity extends Activity implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener{
    public static final String videoPath = Environment.getExternalStorageDirectory().getPath()+"/one.mp4";

    private SurfaceTexture videoTexture;
    private GLSurfaceView glView;
    //private MediaPlayer mediaPlayer;
    private MediaPlayerWrapper mMediaPlayerWrapper = new MediaPlayerWrapper();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NativeMediaWrapper.nativeOnCreate();

        glView = new GLSurfaceView(this);
        setContentView(glView);
        glView.setEGLContextClientVersion(2);
        glView.setRenderer(this);

        mMediaPlayerWrapper.init();
        mMediaPlayerWrapper.setPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer mp) {
                cancelBusy();
            }
        });
        mMediaPlayerWrapper.openRemoteFile(videoPath);
        mMediaPlayerWrapper.prepare();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        NativeMediaWrapper.nativeSurfaceCreated();
        videoTexture = NativeMediaWrapper.nativeGetSurfaceTexture();
        if (videoTexture != null) {
            videoTexture.setOnFrameAvailableListener(this);
        }
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        NativeMediaWrapper.nativeSurfaceChanged(width, height);

        playVideo();
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        NativeMediaWrapper.nativeDrawFrame();
    }


    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        NativeMediaWrapper.nativeFrameAailable();
    }


    private void playVideo() {
        Surface surface = new Surface(videoTexture);
        mMediaPlayerWrapper.getPlayer().setSurface(surface);
        surface.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mMediaPlayerWrapper != null){
            mMediaPlayerWrapper.onPause();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // NativeMediaWrapper.nativeOnDestroy();
        if(mMediaPlayerWrapper != null){
            mMediaPlayerWrapper.getPlayer().setSurface(null);
            mMediaPlayerWrapper.onDestroy();
            mMediaPlayerWrapper = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMediaPlayerWrapper.onResume();
    }

    public void cancelBusy(){
        //findViewById(R.id.progress).setVisibility(View.GONE);
    }
}
